import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import Index from '@/views/Home/index.vue';
import Login from '@/views/Login/index.vue';
import NotFound from '@/views/NotFound/index.vue';
import CodeTrade from '@/views/CodeTrade/index.vue';
import Project from '@/views/CodeTrade/children/Project/index.vue';
import PublishProject from '@/views/CodeTrade/children/PublishProject/index.vue';
import CodeEntrust from '@/views/CodeEntrust/index.vue';
import Entrust from '@/views/CodeEntrust/children/Entrust/index.vue';
import PublishEntrust from '@/views/CodeEntrust/children/publishEntrust/index.vue';
import Communication from '@/views/Communication/index.vue';
import Article from '@/views/Communication/children/Article/index.vue';
import UserInfoView from '@/views/UserInfoView/index.vue';
import UserInfoViewPersonData from '@/views/UserInfoView/children/PersonData/index.vue';
import UserInfoViewAccountSetting from '@/views/UserInfoView/children/AccountSetting/index.vue';
import UserInfoViewProject from '@/views/UserInfoView/children/Project/index.vue';
// import UserInfoViewEntrust from '@/views/UserInfoView/children/Entrust/index.vue';
import UserInfoViewCommunication from '@/views/UserInfoView/children/Communication/index.vue';
import UserInfoViewEntrustMyPosted from '@/views/UserInfoView/children/Entrust/MyPosted/index.vue';
import UserInfoViewEntrustMyPickedUp from '@/views/UserInfoView/children/Entrust/MyPickedUp/index.vue';
import Chat from '@/views/ChatView/index.vue';

const routes: Array<RouteRecordRaw> = [
  { // 首页
    path: '/',
    name: 'Index',
    meta: {
      title: '首页',
      keepAlive: true,
      requireAuth: true,
    },
    component: Index
  },
  {
    path: '/404',
    name: 'NotFound',
    meta: {
      title: 'Page not found',
    },
    component: NotFound
  },
  { // 登录/注册页
    path: '/login',
    name: 'Login',
    meta: {
      title: '登录',
      keepAlive: true,
      requireAuth: false,
      footerClose: true,
      navbarClose: true,
    },
    component: Login
  },
  { // 代码橱窗
    path: '/codeTrade',
    name: 'CodeTrade',
    meta: {
      title: '代码橱窗',
      keepAlive: true,
      requireAuth: false,
      auth: true,
    },
    component: CodeTrade
  },
  { // 代码橱窗-商品
    path: '/codeTrade/project/:id',
    name: 'Project',
    meta: {
      title: '代码橱窗-项目',
      keepAlive: true,
      requireAuth: false,
      auth: true,
    },
    component: Project
  },
  { // 代码橱窗-发布项目
    path: '/codeTrade/publishProject',
    name: 'PublishProject',
    meta: {
      title: '代码橱窗-项目发布',
      keepAlive: true,
      requireAuth: false,
      auth: true,
      footerClose: true,
      navbarClose: true,
    },
    component: PublishProject
  },
  { // 需求委托
    path: '/codeEntrust',
    name: 'CodeEntrust',
    meta: {
      title: '需求委托',
      keepAlive: true,
      requireAuth: false,
      auth: true,
    },
    component: CodeEntrust
  },
  { // 需求委托 委托
    path: '/codeEntrust/entrust/:id',
    name: 'Entrust',
    meta: {
      title: '需求委托-委托',
      keepAlive: true,
      requireAuth: false,
      auth: true,
    },
    component: Entrust
  },
  { // 需求委托-发布委托
    path: '/codeEntrust/publishEntrust',
    name: 'PublishEntrust',
    meta: {
      title: '需求委托-发布委托',
      keepAlive: true,
      requireAuth: false,
      auth: true,
      footerClose: true,
      navbarClose: true,
    },
    component: PublishEntrust
  },
  { // 交流讨论
    path: '/communication',
    name: 'Communication',
    meta: {
      title: '交流讨论',
      keepAlive: true,
      requireAuth: false,
      auth: true,
    },
    component: Communication,
  },
  { // 交流讨论-文章
    path: '/communication/article/:id',
    name: 'Article',
    meta: {
      title: '交流讨论-文章',
      keepAlive: true,
      requireAuth: false,
      auth: true,
    },
    component: Article
  },
  { // 个人中心
    path: '/userInfoView',
    name: 'UserInfoView',
    redirect: '/userInfoView/personData',
    meta: {
      title: '个人中心',
      keepAlive: true,
      requireAuth: false,
      auth: true,
    },
    component: UserInfoView,
    children: [
      { // 个人资料
        path: 'personData',
        name: 'UserInfoView-PersonData',
        meta: {
          title: '个人信息-个人资料',
          keepAlive: true,
          requireAuth: false,
          auth: true,
        },
        component: UserInfoViewPersonData
      },
      { // 账号设置
        path: 'accountSetting',
        name: 'UserInfoView-AccountSetting',
        meta: {
          title: '个人信息-账号设置',
          keepAlive: true,
          requireAuth: false,
          auth: true,
        },
        component: UserInfoViewAccountSetting
      },
      { // 项目信息
        path: 'personData',
        name: 'UserInfoView-Project',
        meta: {
          title: '个人中心-项目信息',
          keepAlive: true,
          requireAuth: false,
          auth: true,
        },
        component: UserInfoViewProject
      },
      { // 委托信息-我发布的
        path: 'MyPosted',
        name: 'UserInfoView-Entrust-MyPosted',
        meta: {
          title: '个人中心-委托信息-我发布的',
          keepAlive: true,
          requireAuth: false,
          auth: true,
        },
        component: UserInfoViewEntrustMyPosted
      },
      { // 委托信息-我接取的
        path: 'MyPickedUp',
        name: 'UserInfoView-Entrust-MyPickedUp',
        meta: {
          title: '个人中心-委托信息-我接取的',
          keepAlive: true,
          requireAuth: false,
          auth: true,
        },
        component: UserInfoViewEntrustMyPickedUp
      },
      { // 交流贴信息
        path: 'personData',
        name: 'UserInfoView-Communication',
        meta: {
          title: '个人中心-交流贴信息',
          keepAlive: true,
          requireAuth: false,
          auth: true,
        },
        component: UserInfoViewCommunication
      },
    ],
  },
  // 聊天页面
  {
    path: '/chat',
    name: 'Chat',
    meta: {
      title: '聊天页面',
      keepAlive: true,
      requireAuth: false,
      auth: true,
    },
    component: Chat
  },
  // 所有未定义路由，全部重定向到404页
  {
    path: '/:catchAll(.*)',
    redirect: '/404'
  }

]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局守卫：登录拦截 本地没有存token,请重新登录
router.beforeEach((to, from, next) => {
  // 判断有没有登录
  // if (!localStorage.getItem('token')) {
  // 	if (to.name == "login") {
  // 		next();
  // 	} else {
  // 		router.push('login')
  // 	}
  // } else {
  // 	next();
  // }

  // 导航栏对应路由高亮显示
  if (to.name?.toString().split('/')[0] == "Index" || to.name?.toString().split('/')[0] == "") {
    sessionStorage.setItem('navbarActive', '1')
    next();
  } else if (to.name == "CodeTrade") {
    sessionStorage.setItem('navbarActive', '2')
    next();
  } else if (to.name == "CodeEntrust") {
    sessionStorage.setItem('navbarActive', '3')
    next();
  } else if (to.name == "CodeTrade") {
    sessionStorage.setItem('Communication', '4')
    next();
  } else {
    next();
  }
});

export default router;